/**
 * Class:   CurriculumActivityManager
 * 
 * Methods:
 * 
 *      public String insertActivity( CurriculumActivity activity );
 *      
 *      public ArrayList<CurriculumActivity> searchActivity
 *      		( CurriculumActivity activity );
 *      
 *      public ArrayList<CurriculumActivity> generateActivityList
 *      		( CurriculumActivity activity );
 *      
 *      public String updateActivity( CurriculumActivity original,
 *      							  CurriculumActivity changed );
 *      
 *      public int getActivityID( CurriculumActivity activity );
 *      
 *      private boolean lackingRequirements( CurriculumActivity activity );
 * 
 * Remarks:
 * 
 * 		This class can be touched by the controllers.
 * 
 * @author ( Sharmaine Lim )
 * @version ( 2013-02-28 // YYYY-MM-DD )
 */

package managers;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import models.CurriculumActivity;
import models.CurriculumParticipant;
import models.Participant;

public class CurriculumActivityManager extends DatabaseManager {
    
    /**
     * Constructors
     */
    
    public CurriculumActivityManager() {}
    
    
    
    /**
     *  Insert Activity
     *  	- inserts an activity into the curriculum activity table
     *  
     *  Parameters
     *		- CurriculumActivity activity: the activity to be added
     *	
     *	Returns
     *		- String "Lacking Requirements": requirements
     *				were not fulfilled
     *		- String "Success": activity is successfully inserted
     * 		- String "Activity": insertion failed for the activity table
     * 		- String "Beneficiary": insertion failed for the beneficiary
     * 				table ( new beneficiaries )
     * 		- String "Activity_Beneficiary": insertion failed for the
     * 				c_activityBeneficiary table
     * 		- String "Participant": insertion failed for the participant
     * 				table ( new participants )
     * 		- String "Activity_Participant": insertion failed for the
     * 				c_activityParticipant table
     * 		- String "Remark": insertion failed for the v_Remark table
     */
    public String insertActivity( CurriculumActivity activity ) {
    	if ( lackingRequirements( activity ) ) {
    		return "Lacking Requirements";
    	}
    	
    	try {
    		/* Declaration of Block Variables */
    		PreparedStatement preparedStatement;
    		int activityID;
    		ArrayList<String> beneficiaryList;
    		ArrayList<CurriculumParticipant> participantList;
    		ArrayList<String> remarkList;
    		
    		/* Insert activity data into `curriculumActivity` table */
    		connect();
    		
    		preparedStatement = getConnection()
    							.prepareStatement( "INSERT INTO" +
    											   " `volunteerActivity`" +
    											   " (`title`, `activityType`," +
    											   " `dateStart`, `dateEnd`," +
    											   " `courseCode`) VALUES" +
    											   " (?, ?, ?, ?, ?)" );

            preparedStatement.setString( 1, activity.getTitle() );
            preparedStatement.setString( 2, activity.getType() );
            
            preparedStatement.setString( 3, activity.getDateStart()
            								.toString() );
            preparedStatement.setString( 4, activity.getDateEnd()
            								.toString() );
            preparedStatement.setString( 5, activity.getCourseCode()
            								.toString() );
            preparedStatement.executeUpdate();
            
            disconnect();
            
            activityID = getActivityID( activity );
            
            if ( activityID == 0 ) {
            	return "Activity";
            }
            
            /* Add beneficiary link to the database */
            beneficiaryList = activity.getBeneficiaries();
            
            for ( String beneficiary : beneficiaryList ) {
            	
        		/* Declaration of Block Variables */
            	int beneficiaryID = new BeneficiaryManager()
            						.getBeneficiaryID( beneficiary );
            	
            	if ( beneficiaryID == 0 ) {
            		
            		if ( !( new BeneficiaryManager()
            				.insertBeneficiary( beneficiary ) ) ) {
            			return "Beneficiary";
            		}
            		
            		beneficiaryID = new BeneficiaryManager()
            						.getBeneficiaryID( beneficiary );
            		
            	}
        		
            	/* Insert relation into `c_activityBeneficiary` table */
            	if ( !( new CurriculumActivityRelationsManager()
            		    .insertActivityBeneficiaryRelation( activityID,
            		    									beneficiaryID ) ) ) {
            		return "Activity_Beneficiary";
            	}
            }
            
            /* Add participant link into the database */
            participantList = activity.getParticipants();
            
            for ( CurriculumParticipant participant : participantList ) {
            	
            	/* Insert relation into `c_activityParticipant` table */
            	if ( !( new CurriculumActivityRelationsManager()
            		    .insertCurriculumParticipant( activityID,
            		    							  participant ) ) ) {
            		return "Activity_Participant";
            	}
            }
            
            /* Add remark links into the database */
            remarkList = activity.getRemarks();
            
            if ( remarkList != null ) {
	            for ( String remark : remarkList ) {
	        		
	            	/* Insert remark into `c_remark` table */
	        		if ( !( new RemarkManager().insertRemark( activityID,
	        												  remark,
	        												  "Curriculum" ) ) ) {
	        			return "Remark";
	        		}
	            	
	            }
            }
            
            disconnect();
            return "Success";
    	}
    	catch ( Exception ex ) {
    		ex.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return "Activity";
    }
    
    
    
    /**
     *  Search for Activity
     *  	- generates the list of activities in the activity table based
     *  	  on the given specifications
	 * 
	 * Searchables
	 * 		- activity title
	 * 		- activity type
	 * 		- date start
	 * 		- date end
	 * 		- course code
	 * 		- remarks
	 * 		- beneficiary name
     *  
     *  Parameters
     *		- CurriculumActivity activity: the activity
     *				specifications
     *	
     *	Returns
     *		- ArrayList<CurriculumActivity>.size() == 0: no activities found
     * 		- ArrayList<CurriculumActivity>.size() > 0: list of activities found
     */ 
    public ArrayList<CurriculumActivity> searchActivity( CurriculumActivity activity ) {
    	
		/* Declaration of Local Variables */
    	ArrayList<CurriculumActivity> activityList = new ArrayList<CurriculumActivity>();
    	int notNull = 0;
    	
    	try {
    		/* Declaration of Block Variables */
    		String query;
    		Statement statement;
    		ResultSet resultSet;
    		
    		connect();
    		
    		query = "SELECT * FROM `curriculumActivity` WHERE";
    		
    		/* Activity Title */
    		if ( activity.getTitle() != null &&
    			 !activity.getTitle().equals( "" ) ) {
    			
    			query = query.concat( " `title` LIKE \"%" +
    								  activity.getTitle() + "%\"" );
    			notNull++;
    		}
    		
    		/* Activity Type */
    		if ( activity.getType() != null &&
    			 !activity.getType().equals( "" ) ) {
    			
    			query = query.concat( addAnd(notNull) +
    								  " `activityType` LIKE \"%" +
    								  activity.getType() + "%\"" );
    			notNull++;
    		}
    		
    		/* Date Start */
    		if ( activity.getDateStart() != null &&
    			 !activity.getDateStart().toString().equals("1990-1-0") ) {
    			query = query.concat( addAnd(notNull) + " `dateStart` = \"" +
    								  activity.getDateStart().toString() +
    								  "\"" );
    			notNull++;
    		}
    		
    		/* Date End */
    		if ( activity.getDateEnd() != null &&
       			 !activity.getDateEnd().toString().equals("1990-1-0") ) {
    			query = query.concat( addAnd(notNull) + " `dateEnd` = \"" +
    								  activity.getDateEnd().toString() +
    								  "\"" );
    			notNull++;
    		}
    		
    		/* Course Code */
    		if ( activity.getCourseCode() != null &&
    			 !activity.getCourseCode().equals( "" ) ) {
    			
    			query = query.concat( addAnd(notNull) + " `courseCode` = \"" +
    								  activity.getCourseCode() + "\"" );
    			notNull++;
    		}
    		
    		/* Remarks */
    		if ( activity.getRemarks() != null &&
    			 activity.getRemarks().size() > 0 ) {
    			
    			if ( !activity.getRemarks().get( 0 ).equals( "" ) ) {
    				query = query.concat( addAnd(notNull) + " `activityID` IN" +
    									  " (SELECT `activityID`" +
    									  " FROM `c_remark` WHERE" +
    									  " `remark` LIKE \"%" +
    									  activity.getRemarks().get( 0 )+
    									  "%\")" );
        			notNull++;
    			}
    		}
    		
    		/* Beneficiary */
    		if ( activity.getBeneficiaries() != null &&
    			 activity.getBeneficiaries().size() > 0 ) {
    			
    			if ( !activity.getBeneficiaries().get( 0 ).equals( "" ) ) {
    				query = query.concat( addAnd(notNull) + " `activityID` IN" +
    									  " (SELECT `activityID`" +
    									  " FROM `c_activityBeneficiary`" +
    									  " WHERE `beneficiaryID` IN" +
    									  " (SELECT `beneficiaryID`" +
    									  " FROM `beneficiary` WHERE" +
    									  " `beneficiaryName` = \"" +
    									  activity.getBeneficiaries().get( 0 ) +
    									  "\"" );
        			
        			notNull++;
    			}
    		}
    		
    		statement = getConnection().createStatement();
    		
    		System.out.println( query );
    		
    		resultSet = statement.executeQuery( query );
    		
    		while ( resultSet.next() ) {
    			
        		/* Declaration of Block Variables */
    			int activityID = resultSet.getInt( "activityID" );
    			CurriculumActivity currentActivity;
    			
    			currentActivity = new CurriculumActivity
    							  ( resultSet.getString( "title" ),
    								resultSet.getString( "activityType" ),
    								new BeneficiaryManager()
    							  		.searchBeneficiary( activityID ),
    								new RemarkManager()
    							  		.searchRemark( activityID, "Curriculum" ),
    								parseDate( resultSet
    										   .getDate( "dateStart" ) ),
    								parseDate( resultSet
    										   .getDate( "dateEnd" ) ),
    								resultSet.getString( "courseCode" ),
    								new CurriculumActivityRelationsManager()
    							  		.searchParticipants( activityID ) );
    			
    			activityList.add( currentActivity );
    		}
    		
    	}
    	catch ( Exception e ) {
    		e.printStackTrace();
    	}
    	finally {
	    	if ( getConnection() != null ) {
	    		disconnect();
	    	}
    	}
    	
    	return activityList;
    }
    
    
    
    /**
     *  Generate Activity List
     *  	- generates the list of activities in the activity table based
     *  	  on the given specifications
	 * 
	 * Searchables
	 * 		- activity type
	 * 		- date range
	 * 		- course code
	 * 		- beneficiary name
     *  
     *  Parameters
     *		- CurriculumActivity activity: the activity specifications
     *									   (where the dateStart is the
     *										starting range, and dateEnd
     *										is the ending range) 
     *	
     *	Returns
     *		- ArrayList<CurriculumActivity>.size() == 0: no activities found
     * 		- ArrayList<CurriculumActivity>.size() > 0: list of activities found
     */ 
    public ArrayList<CurriculumActivity> generateActivityList( CurriculumActivity activity ) {
    	
		/* Declaration of Local Variables */
    	ArrayList<CurriculumActivity> activityList = new ArrayList<CurriculumActivity>();
    	int notNull = 0;
    	
    	try {
    		/* Declaration of Block Variables */
    		String query;
    		Statement statement;
    		ResultSet resultSet;
    		
    		connect();
    		
    		query = "SELECT * FROM `curriculumActivity` WHERE";
    		
    		/* Activity Title */
    		if ( activity.getTitle() != null &&
    			 !activity.getTitle().equals( "" ) ) {
    			
    			query = query.concat( " `title` LIKE \"%" +
    								  activity.getTitle() + "%\"" );
    			notNull++;
    		}
    		
    		/* Activity Type */
    		if ( activity.getType() != null &&
    			 !activity.getType().equals( "" ) ) {
    			
    			query = query.concat( addAnd(notNull) +
    								  " `activityType` LIKE \"%" +
    								  activity.getType() + "%\"" );
    			notNull++;
    		}
    		
    		/* Date Range of Activities */
    		if ( activity.getDateStart() != null &&
    			 !activity.getDateStart().toString().equals("1990-1-0") &&
    			 activity.getDateEnd() != null &&
    			 !activity.getDateEnd().toString().equals("1990-1-0") ) {
    			query = query.concat( addAnd(notNull) + " `dateStart` = \"" +
    								  activity.getDateStart().toString() +
    								  "\"" );
    			notNull++;
    		}
    		
    		/* Course Code */
    		if ( activity.getCourseCode() != null &&
    			 !activity.getCourseCode().equals( "" ) ) {
    			
    			query = query.concat( addAnd(notNull) + " `courseCode` = \"" +
    								  activity.getCourseCode() + "\"" );
    			notNull++;
    		}
    		
    		/* Beneficiary */
    		if ( activity.getBeneficiaries() != null &&
    			 activity.getBeneficiaries().size() > 0 ) {
    			
    			if ( !activity.getBeneficiaries().get( 0 ).equals( "" ) ) {
    				query = query.concat( addAnd(notNull) + " `activityID` IN" +
    									  " (SELECT `activityID`" +
    									  " FROM `c_activityBeneficiary`" +
    									  " WHERE `beneficiaryID` IN" +
    									  " (SELECT `beneficiaryID`" +
    									  " FROM `beneficiary` WHERE" +
    									  " `beneficiaryName` = \"" +
    									  activity.getBeneficiaries().get( 0 ) +
    									  "\"" );
        			
        			notNull++;
    			}
    		}
    		
    		statement = getConnection().createStatement();
    		
    		System.out.println( query );
    		
    		resultSet = statement.executeQuery( query );
    		
    		while ( resultSet.next() ) {
    			
        		/* Declaration of Block Variables */
    			int activityID = resultSet.getInt( "activityID" );
    			CurriculumActivity currentActivity;
    			
    			currentActivity = new CurriculumActivity
    							  ( resultSet.getString( "title" ),
    								resultSet.getString( "activityType" ),
    								new BeneficiaryManager()
    							  		.searchBeneficiary( activityID ),
    								new RemarkManager()
    							  		.searchRemark( activityID, "Curriculum" ),
    								parseDate( resultSet
    										   .getDate( "dateStart" ) ),
    								parseDate( resultSet
    										   .getDate( "dateEnd" ) ),
    								resultSet.getString( "courseCode" ),
    								new CurriculumActivityRelationsManager()
    							  		.searchParticipants( activityID ) );
    			
    			activityList.add( currentActivity );
    		}
    		
    	}
    	catch ( Exception e ) {
    		e.printStackTrace();
    	}
    	finally {
	    	if ( getConnection() != null ) {
	    		disconnect();
	    	}
    	}
    	
    	return activityList;
    }
    
    
    
    /**
     *  Update Activity
     *  	- updates an activity in the curriculum activity table
     *  
     *  Parameters
     *		- CurriculumActivity original: the original version of
     *				the activity
     *		- CurriculumActivity changed: the changed version of
     *				the activity
     *	
     *	Returns
     *		- String "Original activity does not exist.": the activity is
     *				not found in the database
     *		- String "Lacking Requirements": requirements
     *				were not fulfilled
     *		- String "Success": activity is successfully updated
     * 		- String "Activity": updating failed for the activity table
     * 		- String "Beneficiary": updating failed for the beneficiary
     * 				table ( new beneficiaries )
     * 		- String "Activity_Beneficiary": updating failed for the
     * 				c_activityBeneficiary table
     * 		- String "Participant": updating failed for the participant
     * 				table ( new participants )
     * 		- String "Activity_Participant": updating failed for the
     * 				c_activityParticipant table
     * 		- String "Remark": updating failed for the c_Remark table
     */
    public String updateActivity( CurriculumActivity original,
    							  CurriculumActivity changed ) {
		
    	/* Declaration of Local Variables */
        int activityID = getActivityID( original );
        
        if ( activityID == 0 ) {
        	return "Original activity does not exist.";
        }
        
    	if ( lackingRequirements( changed ) ) {
    		return "Lacking Requirements";
    	}
    	
    	try {
    		/* Declaration of Block Variables */
    		PreparedStatement preparedStatement;
            ArrayList<String> beneficiaryList;
            ArrayList<String> newBeneficiaryList;
            ArrayList<CurriculumParticipant> participantList;
            ArrayList<CurriculumParticipant> newParticipantList;
            ArrayList<String> remarkList;
            ArrayList<String> newRemarkList;
            
    		/* Update activity data in `curriculumActivity` table */
    		connect();
    		
            /** Please remove this part (PreparedStatement) if it doesn't work. */
    		preparedStatement = getConnection()
    							.prepareStatement( "UPDATE" +
    											   " `curriculumActivity`" +
    											   " SET `title` = ?," +
    											   " `activityType` = ?," +
    											   " `dateStart` = ?," +
    											   " `dateEnd` = ?," +
    											   " `courseCode` = ?" );

            preparedStatement.setString( 1, changed.getTitle() );
            preparedStatement.setString( 2, changed.getType() );
            preparedStatement.setString( 3, changed.getDateStart().toString() );
            preparedStatement.setString( 4, changed.getDateEnd().toString() );
            preparedStatement.setString( 5, changed.getCourseCode().toString() );
            preparedStatement.executeUpdate();
    		//*/
    		
            /** Please remove this part (Statement) if the PreparedStatement works. *//*
            Statement statement = getConnection().createStatement();
            
            int canceled = 0;
            
            if ( changed.isCanceled() ) {
            	canceled = 1;
            }
            
            statement.executeUpdate( "UPDATE `curriculumActivity` SET" +
    								 " `title` = \"" + changed.getTitle() +
    								 "\", `activityType` = \"" +
    								 changed.getType() + "\", `dateStart` = \"" +
    								 changed.getDateStart().toString() + "\"," +
    								 " `dateEnd` = \"" +
    								 changed.getDateEnd().toString() + "\"," +
    								 " `courseCode` = \"" +
    								 changed.getCourseCode() + "\"" );
            //*/
    		
            disconnect();
            
            if ( getActivityID( changed ) != activityID ) {
            	return "Activity";
            }
            
            /* Update beneficiary links in database */
            beneficiaryList = original.getBeneficiaries();
            newBeneficiaryList = changed.getBeneficiaries();
            
            for ( String beneficiary : beneficiaryList ) {
        		
            	/* Declaration of Block Variables */
            	boolean found = false;
            	
            	for ( String newBeneficiary : newBeneficiaryList ) {
            		if ( beneficiary.equals( newBeneficiary ) ) {
            			found = true;
            		}
            	}
            	
            	if ( !found ) {
            		
            		/* Declaration of Block Variables */
                	int beneficiaryID = new BeneficiaryManager()
                						.getBeneficiaryID( beneficiary );
                	
                	if ( beneficiaryID != 0 ) {
                		/* Delete relation from `c_activityBeneficiary` table */
                		if ( !( new CurriculumActivityRelationsManager()
                			.deleteActivityBeneficiaryRelation( activityID,
                												beneficiaryID ) ) ) {
                			return "Activity_Beneficiary";
                		}
                	}
            	}
            }
            
            for ( String newBeneficiary : newBeneficiaryList ) {
            	
        		/* Declaration of Block Variables */
            	boolean found = false;
            	
            	for ( String beneficiary : beneficiaryList ) {
            		if ( newBeneficiary.equals( beneficiary ) ) {
            			found = true;
            		}
            	}
            	
            	if ( !found ) {
                	
            		/* Declaration of Block Variables */
                	int beneficiaryID = new BeneficiaryManager()
                						.getBeneficiaryID( newBeneficiary );
                	
                	if ( beneficiaryID == 0 ) {
                		if ( !( new BeneficiaryManager()
                				.insertBeneficiary( newBeneficiary ) ) ) {
                			return "Beneficiary";
                		}
                		
                		beneficiaryID = new BeneficiaryManager()
                						.getBeneficiaryID( newBeneficiary );
                	}
            		
            		/* Insert relation into `c_activityBeneficiary` table */
            		if ( !( new CurriculumActivityRelationsManager()
            			    .insertActivityBeneficiaryRelation( activityID,
            			    									beneficiaryID ) ) ) {
            			return "Activity_Beneficiary";
            		}
            	}
            }
            
            /* Update participant links in database */
            participantList = original.getParticipants();
            newParticipantList = changed.getParticipants();
            
            for ( CurriculumParticipant participant : participantList ) {
            	
        		/* Declaration of Block Variables */
            	boolean found = false;
            	
            	for ( CurriculumParticipant newParticipant : newParticipantList ) {
            		if ( participant.getName()
            						.equals( newParticipant.getName() ) ) {
            			
            			found = true;
            		}
            	}
            	
            	if ( !found ) {
                	
            		/* Declaration of Block Variables */
                	int participantID = new ParticipantManager()
                						.getParticipantID
                						( (Participant)participant );
                	
                	if ( participantID != 0 ) {
                		/* Delete relation from `c_activityParticipant` table */
                		if ( !( new CurriculumActivityRelationsManager()
                			    .deleteCurriculumParticipant( activityID,
                			    							  participantID ) ) ) {
                			return "Activity_Participant";
                		}
                	}
            	}
            }
            
            for ( CurriculumParticipant newParticipant : newParticipantList ) {
            	
        		/* Declaration of Block Variables */
            	boolean found = false;
            	
            	for ( CurriculumParticipant participant : participantList ) {
            		if ( newParticipant.getName()
            						   .equals( participant.getName() ) ) {
            			found = true;
            		}
            	}
            	
            	if ( !found ) {
                	/* Insert relation into `c_activityParticipant` table */
            		if ( !( new CurriculumActivityRelationsManager()
            				.insertCurriculumParticipant( activityID,
            											  newParticipant ) ) ) {
            			return "Activity_Participant";
            		}
            	}
            }
            
            /* Update remark links in database */
            remarkList = original.getRemarks();
            newRemarkList = changed.getRemarks();
            
            for ( String remark : remarkList ) {
            	
        		/* Declaration of Block Variables */
            	boolean found = false;
            	
            	for ( String newRemark : newRemarkList ) {
            		if ( remark.equals( newRemark ) ) {
            			found = true;
            		}
            	}
            	
            	if ( !found ) {
                	/* Delete remark from `c_remark` table */
                	if ( !( new RemarkManager().deleteRemark( activityID,
                											  remark,
                											  "Curriculum" ) ) ) {
                		return "Remark";
                	}
            	}
            }
            
            for ( String newRemark : newRemarkList ) {
            	
        		/* Declaration of Block Variables */
            	boolean found = false;
            	
            	for ( String remark : remarkList ) {
            		if ( newRemark.equals( remark ) ) {
            			found = true;
            		}
            	}
            	
            	if ( !found ) {
                	/* Insert remark into `c_remark` table */
            		if ( !( new RemarkManager().insertRemark( activityID,
            												  newRemark,
            												  "Curriculum" ) ) ) {
            			return "Remark";
            		}
            	}
            }
            
            return "Success";
    	}
    	catch ( Exception ex ) {
    		ex.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return "Activity";
    }
    
    
    
    /**
     *  Delete Activity
     *  	- deletes an activity from the curriculum activity table
     *  
     *  Parameters
     *		- CurriculumActivity activity: the activity to be deleted
     *	
     *	Returns
     *		- boolean true: activity is successfully deleted
     *		- boolean false: deletion failed
     */
    public boolean deleteActivity( CurriculumActivity activity ) {
        
        /* Declaration of Local Variables */
        int activityID = getActivityID( activity );
        
        if ( activityID == 0 ) {
        	return false;
        }
        
    	try {
            /* Declaration of Block Variables */
    		PreparedStatement preparedStatement = null;
    		
    		if ( !new CurriculumActivityRelationsManager()
    			 .deleteActivityBeneficiaryRelation( activityID, 0 )) {
    			
    			return false;
    		}
    		
    		if ( !new CurriculumActivityRelationsManager()
    			 .deleteCurriculumParticipant( activityID, 0 ) ) {
			
    			return false;
    		}
    		
    		if ( new RemarkManager().deleteActivityRemark( activityID,
    													   "Curriculum" ) ) {
			
    			return false;
    		}
    		
    		connect();
            
    		preparedStatement = getConnection()
    							.prepareStatement( "DELETE FROM" +
    											   " `curriculumActivity`" +
    											   " WHERE `activityID`" +
    											   " = ?" );
    		
    		preparedStatement.setInt( 1, activityID );
    		preparedStatement.executeUpdate();
    		
    		disconnect();
    		
    		if ( getActivityID( activity ) == 0 ) {
    			return true;
    		}
    	}
    	catch ( Exception ex ) {
    		ex.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
    
    
    
    /**
     *  Get Activity ID
     *  	- finds the ID of the activity in the activity table
     *  
     *  Parameters
     *		- CurriculumActivity activity: the activity to be found
     *	
     *	Returns
     *		- non-zero int: activity ID found
     * 		- int 0: activity not found
     */
    public int getActivityID( CurriculumActivity activity ) {
    	try {
    		/* Declaration of Block Variables */
    		int activityID = 0;
    		Statement statement;
    		ResultSet resultSet;
    		
    		connect();
    		
    		statement = getConnection().createStatement();
    		
    		resultSet = statement.executeQuery( "SELECT `activityID`" +
    											" FROM `curriculumActivity`" +
    											" WHERE `title` = \"" +
    											activity.getTitle() + "\" AND" +
    											" `activityType` = \"" +
    											activity.getType() + "\" AND" +
    											" `dateStart` = \"" +
    											activity.getDateStart().toString() +
    											"\" AND `dateEnd` = \"" +
    											activity.getDateEnd().toString() +
    											"\" AND `courseCode` = \"" +
    											activity.getCourseCode() + "\"" );
    		
    		while ( resultSet.next() ) {
    			activityID = resultSet.getInt( "activityID" );
    		}
    		
    		disconnect();
    		return activityID;
    	}
    	catch ( Exception e ) {
    		e.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return 0;
    }
    
    
    
    /**
     *  Lacking Requirements
     *  	- checks if all requirements are filled
	 * 
     *  Parameters
     *		- CurriculumActivity activity: activity to be
     *				checked
     *	
     *	Returns
     *		- boolean true: activity is lacking requirements
     * 		- boolean false: activity is not lacking any
     * 				requirement
     */
    private boolean lackingRequirements( CurriculumActivity activity ) {
    	if ( activity.getTitle() == null ||
    		 activity.getTitle().equals( "" ) ) {
    		
    		return true;
    	}
    	
    	if ( activity.getType() == null ||
    		 activity.getType().equals( "" ) ) {
    		
    		return true;
    	}
    	
    	if ( activity.getCourseCode() == null ||
    		 activity.getCourseCode().equals( "" ) ) {
    		
    		return true;
    	}
    	
    	if ( activity.getDateStart() == null ||
      		 activity.getDateStart().toString().equals("1990-1-0") ) {
    		return true;
    	}
    	
    	if ( activity.getDateEnd() == null ||
      		 activity.getDateEnd().toString().equals("1990-1-0") ) {
    		return true;
    	}
    	
    	{	
    		/* Declaration of Block Variables */
    		ArrayList<String> beneficiaryList = activity.getBeneficiaries();
	    	
    		if ( beneficiaryList == null || beneficiaryList.size() < 1 ) {
	    		return true;
	    	}
    	}
    	
    	{	
    		/* Declaration of Block Variables */
    		ArrayList<CurriculumParticipant> participantList = activity.getParticipants();
	    	
    		if ( participantList == null || participantList.size() < 1) {
	    		return true;
	    	}
    	}
    	
    	return false;
    }
}
